<?php

/**
 * This is the model class for table "Usuarios.usuarios".
 *
 * The followings are the available columns in table 'Usuarios.usuarios':
 * @property string $id_usuario
 * @property string $fechaa
 * @property string $id_tipo
 * @property string $matricula
 * @property string $correo
 * @property string $titulo
 * @property string $nombres
 * @property string $apellidop
 * @property string $apellidom
 * @property integer $id_area
 * @property string $puesto
 * @property string $nombre_usuario
 * @property string $contrasenia
 *
 * The followings are the available model relations:
 * @property Area $idArea
 * @property Tipos $idTipo
 *http://www.yiiframework.com/doc/api/1.1/CPagination
 */

class UsuariosExternos extends CActiveRecord
{
 
 public static $conection;   
 public function getDbConnection() {
  if(self::$conection!==null) return
   self::$conection; else 
   { self::$conection = Yii::app()->dbusuarios; 
   if(self::$conection instanceof CDbConnection){ 
   self::$conection->setActive(true); return self::$conection; 
   } else 
   throw new CDbException(Yii::t('yii',"Active Record requires a '$conection' CDbConnection application component.")); 
   } 
   } //See more at: http://collectioncode.com/frameworks_php/multiples-conexiones-a-bases-de-datos-con-yii-framework/#sthash.EMjaZZdK.dpuf



	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'usuarios';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id_area', 'numerical', 'integerOnly'=>true),
			array('id_tipo, titulo', 'length', 'max'=>5),
			array('matricula', 'length', 'max'=>8),
			array('correo', 'length', 'max'=>60),
			array('nombres', 'length', 'max'=>50),
			array('apellidop, apellidom, nombre_usuario', 'length', 'max'=>20),
			array('puesto', 'length', 'max'=>40),
			array('contrasenia', 'length', 'max'=>32),
			array('fechaa', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id_usuario, fechaa, id_tipo, matricula, correo, titulo, nombres, apellidop, apellidom, id_area, puesto, nombre_usuario, contrasenia', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'idArea' => array(self::BELONGS_TO, 'Area', 'id_area'),
			'idTipo' => array(self::BELONGS_TO, 'Tipos', 'id_tipo'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_usuario' => 'Id Usuario',
			'fechaa' => 'Fechaa',
			'id_tipo' => 'Id Tipo',
			'matricula' => 'Matricula',
			'correo' => 'Correo',
			'titulo' => 'Titulo',
			'nombres' => 'Nombres',
			'apellidop' => 'Apellidop',
			'apellidom' => 'Apellidom',
			'id_area' => 'Id Area',
			'puesto' => 'Puesto',
			'nombre_usuario' => 'Nombre Usuario',
			'contrasenia' => 'Contrasenia',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		//$criteria->compare('id_usuario',$this->id_usuario,true);
		//$criteria->compare('fechaa',$this->fechaa,true);
		//$criteria->compare('id_tipo',$this->id_tipo,true);
		$criteria->compare('matricula',$this->matricula,true);
		//$criteria->compare('correo',$this->correo,true);
		//$criteria->compare('titulo',$this->titulo,true);
		$criteria->compare('nombres',$this->nombres,true);
		$criteria->compare('apellidop',$this->apellidop,true);
		$criteria->compare('apellidom',$this->apellidom,true);
		$criteria->compare('id_tipo','ALUMN',true);
		//$criteria->compare('id_area',$this->id_area);
		//$criteria->compare('puesto',$this->puesto,true);
		//$criteria->compare('nombre_usuario',$this->nombre_usuario,true);
		//$criteria->compare('contrasenia',$this->contrasenia,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public function getUserNamesNotExists()
		{
			

			$asesoresInternos=Profesores::model()->findAll();
			$keyInterno=array();
			foreach ($asesoresInternos as $row) {
				$keyInterno[]=$row->id_asesorinterno;
			}
	$commando = Yii::app()->dbusuarios->createCommand()
            			->select('*')
            			->from('usuarios')
            			->where(array('not in', 'id_usuario', $keyInterno))
            			->andwhere('id_tipo = :tipo', array('tipo'=>'DOCEN'));
            			
    			
   $registros=array();
			$dr=$commando->query(); 
			$rows=$dr->readAll();
			
			
			foreach ($rows as $row) {
				$item=new UsuariosExternos();
				$item->id_usuario=$row['id_usuario'];
				$item->nombres=$row['nombres'];
				$item->apellidop=$row['apellidop'];
				$item->apellidom=$row['apellidom'];
				$registros[]=$item;
			}

			return $registros;			
		}

    public function getTotalAlumnos()
    {
    $alumnosSistema=Alumnos::model()->findAll();
		$keyInterno= array();
		foreach ($alumnosSistema as $row) {
			$keyInterno[]=$row->id_alumno;
		}
		$commando = Yii::app()->dbusuarios->createCommand()
            			->select('count(*) as total')
            			->from('usuarios')
            			->where(array('not in', 'id_usuario', $keyInterno))
            			->andwhere('id_tipo = :tipo', array('tipo'=>'ALUMN'));
         $dr=$commando->query(); 
			$rows=$dr->readAll();
		return $rows;
            				
    }
	public function getAlumnosNoExiste($limite,$offset)
	{
		$alumnosSistema=Alumnos::model()->findAll();
		$keyInterno= array();
		foreach ($alumnosSistema as $row) {
			$keyInterno[]=$row->id_alumno;
		}
		$commando = Yii::app()->dbusuarios->createCommand()
            			->select('id_usuario,nombres,apellidop,apellidom,matricula')
            			->from('usuarios')
            			->where(array('not in', 'id_usuario', $keyInterno))
            			->andwhere('id_tipo = :tipo', array('tipo'=>'ALUMN'))
            			->limit ($limite,$offset);
            			
    			
   			$registros=array();
			$dr=$commando->query(); 
			$rows=$dr->readAll();
			
			
			foreach ($rows as $row) {
				$item=new UsuariosExternos();
				$item->matricula=$row['matricula'];
				$item->id_usuario=$row['id_usuario'];
				$item->nombres=$row['nombres'];
				$item->apellidop=$row['apellidop'];
				$item->apellidom=$row['apellidom'];
				$registros[]=$item;
			}

			return $registros;			
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return UsuariosExternos the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	

}
